using System;
using System.Collections.Generic;
using System.IO;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.Faces.Base;
using Microsoft.Practices.Unity;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestSuperClassControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        [Test]
        public void TestLayoutSetup()
        {
            //---------------Set up test pack-------------------

            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            IControlHabanero controlHabanero = superClassControl;
            Assert.AreEqual(3, controlHabanero.Controls.Count);
            Assert.AreSame(superClassControl.HasSuperClassCheckBox, controlHabanero.Controls[1]);
            Assert.AreSame(superClassControl.InheritanceRelationshipPanelInfo.Panel, controlHabanero.Controls[0]);

            IPanelInfo panelBuilderInfo = superClassControl.InheritanceRelationshipPanelInfo;
            Assert.IsNotNull(panelBuilderInfo);
            PanelInfo.FieldInfoCollection mappers = panelBuilderInfo.FieldInfos;
            Assert.IsNotNull(mappers);
            Assert.IsInstanceOf(typeof (IComboBox), mappers["SuperClassID"].ControlMapper.Control);
            Assert.IsInstanceOf(typeof (IComboBox), mappers["InheritanceType"].ControlMapper.Control);
            Assert.IsInstanceOf(typeof (IComboBox), mappers["Discriminator"].ControlMapper.Control);
            Assert.IsInstanceOf(typeof(IComboBox), mappers["IdentityPropertyID"].ControlMapper.Control);
        }

        [Test]
        public void TestHasSuperClassCheckBox()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.HasSuperClassCheckBox.Checked);
        }

        [Test]
        public void Test_LayoutDefaults()
        {
            //---------------Set up test pack-------------------
            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            IPanelInfo panelBuilderInfo = superClassControl.InheritanceRelationshipPanelInfo;
            PanelInfo.FieldInfoCollection mappers = panelBuilderInfo.FieldInfos;

            IComboBox bOInfoIDCombo = (IComboBox) mappers["SuperClassID"].ControlMapper.Control;
            IComboBox inheritanceTypeCombo = (IComboBox) mappers["InheritanceType"].ControlMapper.Control;
            IComboBox discriminatorCombo = (IComboBox) mappers["Discriminator"].ControlMapper.Control;
            IComboBox idCombo = (IComboBox)mappers["IdentityPropertyID"].ControlMapper.Control;

            //Assert.AreEqual(ORMapping.SingleTableInheritance.ToString(), inheritanceTypeCombo.SelectedItem);
            Assert.IsNull(inheritanceTypeCombo.SelectedItem);
            Assert.IsTrue(bOInfoIDCombo.Visible);
            Assert.IsTrue(inheritanceTypeCombo.Visible);
            Assert.IsFalse(discriminatorCombo.Visible);
            Assert.IsFalse(idCombo.Visible);
        }

        [Test]
        public void Test_SuperClassComboItems()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            IComboBox superClassCombo = superClassControl.SuperClassComboBox;
            DMClass dmClassWithInheritanceRelationship = TestUtilsDMClass.CreateDMClassWithInheritanceRelationship();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, superClassCombo.Items.Count);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = dmClassWithInheritanceRelationship;
            //---------------Test Result -----------------------
            Assert.AreEqual(4, superClassCombo.Items.Count);
        }

        //This is failing because the class is not excluded from the list
        [Test]
        public void Test_SuperClassComboItems_DoesNotIncludeItself()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();
            DMClass dmClass = TestUtilsShared.CreateSavedDMInheritanceRelationship().SubClass;
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            
            IComboBox superClassCombo = superClassControl.SuperClassComboBox;
            //---------------Assert Precondition----------------
            Assert.AreEqual(5, superClassCombo.Items.Count);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = dmClass;
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassCombo.Items.Contains(dmClass));
            //Expect 4 items: 3 businessObjects and 1 blank item
            Assert.AreEqual(4, superClassCombo.Items.Count,
                            "Should be 3 items (4 classes less the DMClass) Plus blank line");
        }

        [Test]
        public void Test_SuperClassComboItems_DoesNotInclude_Children()
        {
            //---------------Set up test pack-------------------
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();

            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMClass dmClassChild = TestUtilsShared.CreateSavedDMClass();
            DMClass dmClassGrandChild = TestUtilsShared.CreateSavedDMClass();

            dmClassGrandChild.InheritanceSuperClassRelationship = new DMInheritanceRelationship
                                                           {
                                                               SuperClass = dmClassChild,
                                                               InheritanceType = ORMapping.ClassTableInheritance
                                                           };
            dmClassGrandChild.InheritanceSuperClassRelationship.Save();
            dmClassGrandChild.Save();

            dmClassChild.InheritanceSuperClassRelationship = new DMInheritanceRelationship
                                                      {
                                                          SuperClass = dmClass,
                                                          InheritanceType = ORMapping.ClassTableInheritance
                                                      };
            dmClassChild.InheritanceSuperClassRelationship.Save();
            dmClassChild.Save();

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            IPanelInfo panelBuilderInfo = superClassControl.InheritanceRelationshipPanelInfo;
            PanelInfo.FieldInfoCollection mappers = panelBuilderInfo.FieldInfos;
            //---------------Execute Test ----------------------
            superClassControl.SubClass = dmClass;
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Test Result -----------------------
            IComboBox superClassCombo = (IComboBox) mappers["SuperClassID"].ControlMapper.Control;
            Assert.IsFalse(superClassCombo.Items.Contains(dmClass));
            Assert.IsFalse(superClassCombo.Items.Contains(dmClassChild));
            Assert.IsFalse(superClassCombo.Items.Contains(dmClassGrandChild));
            //Expect 4 items: 3 businessObjects and 1 blank item
            Assert.AreEqual(4, superClassCombo.Items.Count,
                            "Should be 4 items (6 classes less the DMClass and the 2 child classes (6-1-2=3)) Plus blank line");
        }

        [Test]
        public void Test_SuperClassComboItems_WhenNotSaved_DoesNotInclude_Children()
        {
            //---------------Set up test pack-------------------
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();

            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMClass dmClassChild = TestUtilsShared.CreateSavedDMClass();
            DMClass dmClassGrandChild = TestUtilsShared.CreateSavedDMClass();

            dmClassGrandChild.InheritanceSuperClassRelationship = new DMInheritanceRelationship
                                                           {
                                                               SuperClass = dmClassChild,
                                                               InheritanceType = ORMapping.ClassTableInheritance
                                                           };

            dmClassChild.InheritanceSuperClassRelationship = new DMInheritanceRelationship
                                                      {
                                                          SuperClass = dmClass,
                                                          InheritanceType = ORMapping.ClassTableInheritance
                                                      };

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            IPanelInfo panelBuilderInfo = superClassControl.InheritanceRelationshipPanelInfo;
            PanelInfo.FieldInfoCollection mappers = panelBuilderInfo.FieldInfos;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            superClassControl.SubClass = dmClass;
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Test Result -----------------------
            IComboBox superClassCombo = (IComboBox) mappers["SuperClassID"].ControlMapper.Control;
            Assert.IsFalse(superClassCombo.Items.Contains(dmClass));
            Assert.IsFalse(superClassCombo.Items.Contains(dmClassChild));
            Assert.IsFalse(superClassCombo.Items.Contains(dmClassGrandChild));
            //Expect 4 items: 3 businessObjects and 1 blank item
            Assert.AreEqual(4, superClassCombo.Items.Count,
                            "Should be 4 items (6 classes less the DMClass and the 2 child classes (6-1-2=3)) Plus blank line");
        }

        [Ignore("This is ignored because the ApplyChangesToBusinessObject is used to make it work visually")]
        //Brett 29 Jul 2009: Ignored Test - This is ignored because the ApplyChangesToBusinessObject 
        [Test]
        public void Test_SuperClassComboItems_WhenNotSavedAndSetIDProp_DoesNotInclude_Children()
        {
            //---------------Set up test pack-------------------
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();

            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMClass dmClassChild = TestUtilsShared.CreateSavedDMClass();
            DMClass dmClassGrandChild = TestUtilsShared.CreateSavedDMClass();

            dmClassGrandChild.InheritanceSuperClassRelationship = new DMInheritanceRelationship
                                                           {
                                                               SuperClassID = dmClassChild.ClassID,
                                                               InheritanceType = ORMapping.ClassTableInheritance
                                                           };

            dmClassChild.InheritanceSuperClassRelationship = new DMInheritanceRelationship
                                                      {
                                                          SuperClassID = dmClass.ClassID,
                                                          InheritanceType = ORMapping.ClassTableInheritance
                                                      };

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            IPanelInfo panelBuilderInfo = superClassControl.InheritanceRelationshipPanelInfo;
            PanelInfo.FieldInfoCollection mappers = panelBuilderInfo.FieldInfos;

            //---------------Execute Test ----------------------
            superClassControl.SubClass = dmClass;
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Test Result -----------------------
            IComboBox superClassCombo = (IComboBox) mappers["SuperClassID"].ControlMapper.Control;
            //Expect 4 items: 3 businessObjects and 1 blank item
            Assert.AreEqual(4, superClassCombo.Items.Count,
                            "Should be 4 items (6 classes less the DMClass and the 2 child classes) Plus blank line");
            Assert.IsFalse(superClassCombo.Items.Contains(dmClass));
            Assert.IsFalse(superClassCombo.Items.Contains(dmClassChild));
            Assert.IsFalse(superClassCombo.Items.Contains(dmClassGrandChild));
        }

        [Test]
        public void TestORMappingComboBox_HasCorrectOptions()
        {
            //---------------Set up test pack-------------------
            string[] ormappingOptions = Enum.GetNames(typeof (ORMapping));
            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            //---------------Test Result -----------------------
            PanelInfo.FieldInfoCollection mappers = superClassControl.InheritanceRelationshipPanelInfo.FieldInfos;
            IComboBox inheritanceTypeCombo = (IComboBox) mappers["InheritanceType"].ControlMapper.Control;
            Assert.AreEqual(ormappingOptions.Length + 1, inheritanceTypeCombo.Items.Count, "Should have blank at top");
            Assert.AreEqual(ORMapping.SingleTableInheritance.ToString(), inheritanceTypeCombo.Text);

            Dictionary<string, string> inheritanceTypes =
                ClassDef.ClassDefs[typeof (DMInheritanceRelationship)].PropDefcol["InheritanceType"].LookupList.
                    GetLookupList();
            foreach (string ormappingOption in ormappingOptions)
            {
                Assert.IsNotNull(inheritanceTypes[ormappingOption]);
            }
        }

        private static SuperClassControl CreateSuperClassControlWithInheritanceRelationshipLoaded()
        {
            return new SuperClassControl(GlobalUIRegistry.ControlFactory)
                       {
                           SubClass = TestUtilsDMClass.CreateDMClassWithInheritanceRelationship()
                       };
        }

        [Test]
        public void TestConstructControl_HasLoadedDMClasses()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            TestUtilsShared.CreateSavedDMClassWithProps();
            BusinessObjectCollection<DMClass> classesCol =
                BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<DMClass>("");
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(1, classesCol.Count);
            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            //---------------Test Result -----------------------
            Assert.AreEqual(1, classesCol.Count);
            IComboBox superClassComboBox = superClassControl.SuperClassComboBox;
            Assert.AreEqual(2, superClassComboBox.Items.Count, "The Class and a blank");
        }

        [Test]
        public void Test_SubClass_WhenSet_HasLoadedDMClasses()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            TestUtilsShared.CreateSavedDMClassWithProps();
            BusinessObjectCollection<DMClass> classesCol =
                BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<DMClass>("");
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(1, classesCol.Count);
            IComboBox superClassComboBox = superClassControl.SuperClassComboBox;
            Assert.AreEqual(2, superClassComboBox.Items.Count, "The Class and a blank");
            //---------------Execute Test ----------------------
            superClassControl.SubClass = TestUtilsShared.CreateUnsavedValidDMClass();
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Test Result -----------------------
            Assert.AreEqual(1, classesCol.Count);
            Assert.AreEqual(2, superClassComboBox.Items.Count, "The Class and a blank");
        }

        [Test]
        public void Test_SubClass_WhenSet_WithClassWithNoInheritanceRelationship_ShouldBeBlank()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            TestUtilsShared.CreateSavedDMClassWithProps();
            BusinessObjectCollection<DMClass> classesCol =
                BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<DMClass>("");
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            DMClass subClass = TestUtilsShared.CreateUnsavedValidDMClass();
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(1, classesCol.Count);
            Assert.IsNull(superClassControl.InheritanceRelationship);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = subClass;
            //---------------Test Result -----------------------
            Assert.AreSame(subClass, superClassControl.SubClass);
            DMInheritanceRelationship inheritanceRelationship = superClassControl.InheritanceRelationship;
            Assert.IsNull(inheritanceRelationship);
            Assert.IsNull(superClassControl.InheritanceRelationshipPanelInfo.BusinessObject);
        }

        [Test]
        public void Test_SubClass_WhenSet_WithClassWithInheritanceRelationship_ShouldShowInheritanceRelationshipDetails()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            DMClass dmClass2 = TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();

            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();

            DMInheritanceRelationship superClass = new DMInheritanceRelationship();
            dmClass.InheritanceSuperClassRelationship = superClass;
            dmClass.InheritanceSuperClassRelationship.SuperClass = dmClass2;
            dmClass.InheritanceSuperClassRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            dmClass.InheritanceSuperClassRelationship.Save();
            dmClass.Save();

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);

            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.InheritanceSuperClassRelationship);
            Assert.AreSame(superClass, dmClass.InheritanceSuperClassRelationship);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, superClassControl.SubClass);
            Assert.AreSame(superClass, superClassControl.InheritanceRelationshipPanelInfo.BusinessObject);
            Assert.IsTrue(superClassControl.HasSuperClassCheckBox.Checked);
            Assert.AreEqual(dmClass2.ToString(), superClassControl.SuperClassComboBox.SelectedItem);
            Assert.AreEqual(ORMapping.ClassTableInheritance.ToString(),
                            superClassControl.InheritanceTypeComboBox.SelectedItem);
        }

        [Test]
        public void Test_SubClass_WhenSet_WithDifferentClassWithInheritanceRelationship_ShouldNotAffectPreviousDiscriminator()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            DMClass superClass1 = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property1 = superClass1.Properties[1];

            DMClass subClass1 = TestUtilsShared.CreateSavedDMClass();
            DMInheritanceRelationship inheritanceRelationship1 = new DMInheritanceRelationship();
            subClass1.InheritanceSuperClassRelationship = inheritanceRelationship1;
            subClass1.InheritanceSuperClassRelationship.SuperClass = superClass1;
            subClass1.InheritanceSuperClassRelationship.InheritanceType = ORMapping.SingleTableInheritance;
            subClass1.InheritanceSuperClassRelationship.Discriminator = property1.PropertyName;
            subClass1.InheritanceSuperClassRelationship.Save();
            subClass1.Save();

            DMClass superClass2 = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property2 = superClass2.Properties[1];

            DMClass subClass2 = TestUtilsShared.CreateSavedDMClass();
            DMInheritanceRelationship inheritanceRelationship2 = new DMInheritanceRelationship();
            subClass2.InheritanceSuperClassRelationship = inheritanceRelationship2;
            subClass2.InheritanceSuperClassRelationship.SuperClass = superClass2;
            subClass2.InheritanceSuperClassRelationship.InheritanceType = ORMapping.SingleTableInheritance;
            subClass2.InheritanceSuperClassRelationship.Discriminator = property2.PropertyName;
            subClass2.InheritanceSuperClassRelationship.Save();
            subClass2.Save();

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            superClassControl.SubClass = subClass1;

            //---------------Assert Precondition----------------
            Assert.IsNotNull(subClass1.InheritanceSuperClassRelationship);
            Assert.IsNotNull(subClass2.InheritanceSuperClassRelationship);
            Assert.AreSame(inheritanceRelationship1, subClass1.InheritanceSuperClassRelationship);
            Assert.AreSame(inheritanceRelationship2, subClass2.InheritanceSuperClassRelationship);

            Assert.AreSame(subClass1, superClassControl.SubClass);
            Assert.AreSame(inheritanceRelationship1, superClassControl.InheritanceRelationshipPanelInfo.BusinessObject);
            Assert.AreEqual(property1.PropertyName, inheritanceRelationship1.Discriminator);
            Assert.AreEqual(property2.PropertyName, inheritanceRelationship2.Discriminator);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = subClass2;
            //---------------Test Result -----------------------
            Assert.IsNotNull(subClass1.InheritanceSuperClassRelationship);
            Assert.IsNotNull(subClass2.InheritanceSuperClassRelationship);
            Assert.AreSame(inheritanceRelationship1, subClass1.InheritanceSuperClassRelationship);
            Assert.AreSame(inheritanceRelationship2, subClass2.InheritanceSuperClassRelationship);

            Assert.AreSame(subClass2, superClassControl.SubClass);
            Assert.AreSame(inheritanceRelationship2, superClassControl.InheritanceRelationshipPanelInfo.BusinessObject);
            Assert.AreEqual(property1.PropertyName, inheritanceRelationship1.Discriminator);
            Assert.AreEqual(property2.PropertyName, inheritanceRelationship2.Discriminator);
        }

        [Test]
        public void Test_SubClass_WhenNotSet_ShouldHaveDisabledControl()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.Enabled);
        }

        [Test]
        public void Test_SubClass_WhenSetToNotNull_ShouldEnableControl()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            //---------------Assert Precondition----------------
            Assert.IsFalse(superClassControl.Enabled);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = bo;
            //---------------Test Result -----------------------
            Assert.IsTrue(superClassControl.Enabled);
        }

        [Test]
        public void Test_SubClass_WhenSetToNull_ShouldDisableControl()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            superClassControl.SubClass = bo;

            //---------------Assert Precondition----------------
            Assert.IsTrue(superClassControl.Enabled);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.Enabled);
            Assert.AreEqual(1, superClassControl.IDComboBox.Items.Count, "The blank item should still be set but no Props");
        }

        //This should never happen but is written so that the system will recover graciously if it
        // ever lands up in this state.
        [Test]
        public void Test_SubClass_WhenSet_AndHasExistingInheritanceRelationshipThatIsDeleted_ShouldSetNewInheritanceRelationship()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            DMClass dmClass2 = TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();

            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();

            DMInheritanceRelationship superClassRelationship = new DMInheritanceRelationship();
            dmClass.InheritanceSuperClassRelationship = superClassRelationship;
            dmClass.InheritanceSuperClassRelationship.SuperClass = dmClass2;
            dmClass.InheritanceSuperClassRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            dmClass.InheritanceSuperClassRelationship.Save();
            dmClass.Save();
            superClassRelationship.MarkForDelete();
            superClassRelationship.Save();

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.InheritanceSuperClassRelationship);
            Assert.IsTrue(dmClass.InheritanceSuperClassRelationship.Status.IsDeleted);
            Assert.IsTrue(dmClass.InheritanceSuperClassRelationship.Status.IsNew);
            Assert.IsFalse(superClassControl.HasSuperClassCheckBox.Checked);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = dmClass;
            //---------------Test Result -----------------------
            Assert.IsTrue(superClassControl.HasSuperClassCheckBox.Checked);
            DMInheritanceRelationship newInheritanceRelationship = superClassControl.InheritanceRelationship;
            Assert.AreSame(dmClass, newInheritanceRelationship.SubClass);
            Assert.AreNotSame(superClassRelationship, newInheritanceRelationship);
            Assert.AreSame(dmClass.InheritanceSuperClassRelationship, newInheritanceRelationship);
            Assert.IsTrue(dmClass.InheritanceSuperClassRelationship.Status.IsNew);
            Assert.IsFalse(dmClass.InheritanceSuperClassRelationship.Status.IsDeleted);
        }

        //This should b filled with the super class properties as long as they are strings.
        [Test]
        public void TestDiscriminatorHasClassProperties()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            DMClass boWithProps = TestUtilsShared.CreateSavedDMClassWithProps();
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            superClassControl.HasSuperClassCheckBox.Checked = true;
            BusinessObjectCollection<DMClass> classesCol =
                BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<DMClass>("");
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(1, classesCol.Count);
            IComboBox superClassComboBox = superClassControl.SuperClassComboBox;
            Assert.AreEqual(2, superClassComboBox.Items.Count);
            Assert.AreEqual(1, superClassControl.DiscriminatorComboBox.Items.Count);
            DMProperty property = boWithProps.Properties[0];
            Assert.AreEqual(property.PropertyTypeString, "System.String");
            //---------------Execute Test ----------------------


            superClassComboBox.SelectedIndex = GetIndex(superClassComboBox, boWithProps);
            //---------------Test Result -----------------------
            Assert.AreEqual(boWithProps.Properties.Count + 1, superClassControl.DiscriminatorComboBox.Items.Count);
            Assert.AreEqual(property.PropertyName,
                            superClassControl.DiscriminatorComboBox.Items[1].ToString());
        }


        [Test]
        public void TestChangeSuperClass_ReloadsDiscriminatorClassProperties()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            DMClass dmClassWithProps1 = TestUtilsShared.CreateSavedDMClassWithProps();
            DMClass dmClassWithProps2 = TestUtilsShared.CreateSavedDMClassWithProps();
            TestUtilsShared.CreateSavedDMProperty(dmClassWithProps2);

            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            superClassControl.HasSuperClassCheckBox.Checked = true;

            IComboBox superClassComboBox = superClassControl.SuperClassComboBox;
            superClassComboBox.SelectedIndex = GetIndex(superClassComboBox, dmClassWithProps1);
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(dmClassWithProps1.Properties.Count + 1, superClassControl.DiscriminatorComboBox.Items.Count);
            Assert.AreEqual(dmClassWithProps1.Properties[0].PropertyName,
                            superClassControl.DiscriminatorComboBox.Items[1].ToString());
            //---------------Execute Test ----------------------
            superClassComboBox.SelectedIndex = GetIndex(superClassComboBox, dmClassWithProps2);

            //---------------Test Result -----------------------
            Assert.AreEqual(dmClassWithProps2.Properties.Count + 1, superClassControl.DiscriminatorComboBox.Items.Count);
            Assert.AreEqual(dmClassWithProps2.Properties[0].PropertyName,
                            superClassControl.DiscriminatorComboBox.Items[1].ToString());
            Assert.AreEqual(dmClassWithProps2.Properties[1].PropertyName,
                            superClassControl.DiscriminatorComboBox.Items[2].ToString());
        }

        [Test]
        public void Test_WhenSelectDiscriminatorItem_ShouldUpdateDMInheritanceRelationship()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            DMClass dmClassWithProps1 = TestUtilsShared.CreateSavedDMClassWithProps();
            DMClass dmClassWithProps2 = TestUtilsShared.CreateSavedDMClassWithProps();
            TestUtilsShared.CreateSavedDMProperty(dmClassWithProps2);

            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            superClassControl.HasSuperClassCheckBox.Checked = true;

            IComboBox superClassComboBox = superClassControl.SuperClassComboBox;
            superClassComboBox.SelectedIndex = GetIndex(superClassComboBox, dmClassWithProps1);
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(dmClassWithProps1.Properties.Count + 1, superClassControl.DiscriminatorComboBox.Items.Count);
            Assert.AreEqual(dmClassWithProps1.Properties[0].PropertyName,
                            superClassControl.DiscriminatorComboBox.Items[1].ToString());
            //---------------Execute Test ----------------------
            superClassControl.DiscriminatorComboBox.SelectedIndex = 1;

            //---------------Test Result -----------------------
            Assert.AreEqual(superClassControl.DiscriminatorComboBox.SelectedValue.ToString(),
                            superClassControl.InheritanceRelationship.Discriminator);
        }

        [Test]
        public void Test_SelectSuperClass_WhenItHasASuperClassWhichHasDiscriminator_ShouldSetDiscriminator()
        {
            //---------------Set up test pack-------------------

            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty();
            TestUtilsDMProperty.SetDMPropStringType(dmProperty);
            dmProperty.Save();
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass superClass = (DMClass) dmProperty.Class;
            DMClass childClass = TestUtilsShared.CreateSavedDMClass(superClass.Assembly);
            dmInheritanceRelationship.SubClass = childClass;
            dmInheritanceRelationship.SuperClass = superClass;
            dmInheritanceRelationship.Discriminator = dmProperty.PropertyName;
            childClass.Save();
            dmInheritanceRelationship.Save();
            DMClass grandChildClass = TestUtilsShared.CreateSavedDMClass(superClass.Assembly);
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory)
                                                      {
                                                          SubClass = grandChildClass
                                                      };
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Assert Precondition----------------
            Assert.IsNull(superClassControl.SuperClassComboBox.SelectedItem);
            Assert.AreEqual(dmProperty.PropertyName, dmInheritanceRelationship.Discriminator);
            //---------------Execute Test ----------------------
            superClassControl.SuperClassComboBox.SelectedItem = childClass.ToString();
            //---------------Test Result -----------------------
            Assert.IsNotNull(grandChildClass.InheritanceSuperClassRelationship);
            Assert.IsNotNull(grandChildClass.InheritanceSuperClassRelationship.Discriminator);
            Assert.IsNotNull(superClassControl.SuperClassComboBox.SelectedItem);
            Assert.AreEqual(dmProperty, superClassControl.DiscriminatorComboBox.SelectedItem);
        }



        [Test]
        public void Test_DiscriminatorCombo_WhenLoadedWithClassThatHasDiscriminator_ShouldSelectInCombo()
        {
            //---------------Set up test pack-------------------

            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty();
            TestUtilsDMProperty.SetDMPropStringType(dmProperty);
            dmProperty.PropertyName = "MyDiscriminatorProp";
            dmProperty.Save();
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass superClass = (DMClass) dmProperty.Class;
            TestUtilsShared.CreateSavedDMProperty(superClass);
            TestUtilsShared.CreateSavedDMProperty(superClass);
            TestUtilsShared.CreateSavedDMProperty(superClass);
            TestUtilsShared.CreateSavedDMProperty(superClass);
            DMClass childClass = TestUtilsShared.CreateSavedDMClass(superClass.Assembly);
            dmInheritanceRelationship.SubClass = childClass;
            dmInheritanceRelationship.SuperClass = superClass;
            dmInheritanceRelationship.Discriminator = dmProperty.PropertyName;
            childClass.Save();
            dmInheritanceRelationship.Save();
            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = new SuperClassControl(controlFactory)
            {
                SubClass = childClass
            };
            //---------------Test Result -----------------------
            Assert.IsNotNull(superClassControl.DiscriminatorComboBox.SelectedItem);
            Assert.AreEqual(dmProperty.PropertyName, dmInheritanceRelationship.Discriminator);
        }

        [Test]
        public void Test_DiscriminatorCombo_WhenLoadedWithClassThatHasDiscriminator_FromImport_ShouldSelectInCombo()
        {
            //---------------Set up test pack-------------------
            const string xml = @"
                <classes>
                   <class name=""Address"" assembly=""ContactManager.BO"" displayName=""Address"" table=""Address"">
                      <property name=""AddressID"" type=""Guid"" assembly=""System"" 
                       displayName=""Address ID"" readWriteRule=""WriteNew"" databaseField=""AddressID""
                        description="""" 
                        compulsory=""true"" 
                      >
	                  </property>
                      <property name=""AddressType"" type=""String"" assembly=""System"" 
                       displayName=""Address Type"" readWriteRule=""ReadWrite"" databaseField=""AddressType""
                        description="""" 
                      >
	                  </property>
                      <property name=""AddressCategoryID"" type=""Guid"" assembly=""System"" 
                       displayName=""Address Category ID"" readWriteRule=""ReadWrite"" databaseField=""AddressCategoryID""
                        description="""" 
                      >
	                  </property>
                      <property name=""ContactID"" type=""Guid"" assembly=""System"" 
                       displayName=""Contact ID"" readWriteRule=""ReadWrite"" databaseField=""ContactID""
                        description="""" 
                      >
	                  </property>
                      <property name=""MailingListContactID"" type=""Guid"" assembly=""System"" 
                       displayName=""Mailing List Contact ID"" readWriteRule=""ReadWrite"" databaseField=""MailingListContactID""
                        description="""" 
                      >
	                  </property>
                    <primaryKey  >
                      <prop name=""AddressID"" />
                    </primaryKey>
                   </class>
                   <class name=""AddressDetails"" assembly=""ContactManager.BO"" displayName=""Address Details"" table=""AddressDetails"">
		                <superClass class=""Address"" assembly=""ContactManager.BO"" 
			                orMapping=""SingleTableInheritance"" discriminator=""AddressType""/>
                   </class>
                </classes>
                ";


            SolutionCreator solutionCreator = new SolutionCreator();
            DMSolution dmSolution = solutionCreator.CreateSolution();
            dmSolution.Assemblies.ForEach(assembly => assembly.MarkForDelete());
            XmlClassDefsLoader loader = new XmlClassDefsLoader("", new DtdLoader(), new FMDefClassFactory(dmSolution));
            loader.LoadClassDefs(xml);
            dmSolution.DefaultAssembly = dmSolution.Assemblies[0];
            DMSolutionPostLoadProcessor processor = new DMSolutionPostLoadProcessor(new AutoMockingUnityContainer());
            processor.ProcessSolution(dmSolution);
            dmSolution.Save();

            DMClass subClass = dmSolution.AllClasses.Find(dmClass => dmClass.ClassNameBO == "AddressDetails");

            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            SuperClassControl superClassControl = new SuperClassControl(controlFactory)
            {
                SubClass = subClass
            };
            //---------------Test Result -----------------------
            Assert.IsNotNull(superClassControl.DiscriminatorComboBox.SelectedItem);
            Assert.AreEqual("AddressType", subClass.InheritanceSuperClassRelationship.Discriminator);
        }

        private static int GetIndex(IComboBox comboBox, DMClass dmClass)
        {
            for (int count = 0; count < comboBox.Items.Count; count++)
            {
                ComboPair item = (ComboPair) comboBox.Items[count];
                if (item.Key == dmClass.ClassNameBO)
                {
                    return count;
                }
            }
            return -1;
        }
        
        [Test]
        public void TestConcreteTableInheritance_ID_Discrim_NotVisible()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);

            //---------------Execute Test ----------------------
            superClassControl.InheritanceTypeComboBox.SelectedValue = ORMapping.ConcreteTableInheritance.ToString();
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.IDComboBox.Visible);
            Assert.IsFalse(superClassControl.IDLabel.Visible);
            Assert.IsFalse(superClassControl.DiscriminatorComboBox.Visible);
            Assert.IsFalse(superClassControl.DiscriminatorLabel.Visible);
        }

        [Test]
        public void TestSingleTableInheritance_ID_NotVisible_Discrim_Visible()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            //---------------Execute Test ----------------------
            superClassControl.InheritanceTypeComboBox.SelectedValue = ORMapping.SingleTableInheritance.ToString();
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.IDComboBox.Visible);
            Assert.IsFalse(superClassControl.IDLabel.Visible);
            Assert.IsTrue(superClassControl.DiscriminatorComboBox.Visible);
            Assert.IsTrue(superClassControl.DiscriminatorLabel.Visible);
        }

        [Test]
        public void TestClassTableInheritance_Both_ID_Discriminator_Visible()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            //---------------Execute Test ----------------------
            superClassControl.InheritanceTypeComboBox.SelectedValue = ORMapping.ClassTableInheritance.ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(superClassControl.IDComboBox.Visible);
            Assert.IsTrue(superClassControl.IDLabel.Visible);
            Assert.IsTrue(superClassControl.DiscriminatorComboBox.Visible);
            Assert.IsTrue(superClassControl.DiscriminatorLabel.Visible);
        }

        [Test]
        public void TestSingleTableInheritance_HasOnlyDiscriminatorCombo()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = CreateSuperClassControlWithInheritanceRelationshipLoaded();
            //---------------Execute Test ----------------------

            superClassControl.InheritanceTypeComboBox.SelectedValue = ORMapping.SingleTableInheritance.ToString();
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.IDComboBox.Visible);
            Assert.IsTrue(superClassControl.DiscriminatorComboBox.Visible);

            //---------------Tear Down -------------------------
        }

        //This should be filled with the parent's ID prop + the child props.
        [Test]
        public void TestIDCombo_WhenHasSuperClass_HasAllProperties()
        {
            //---------------Set up test pack-------------------
            DMClass superClass = TestUtilsShared.CreateSavedDMClassWithProps();
            TestUtilsShared.CreateSavedObjectIdentityInfo(superClass);
            DMClass subClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMInheritanceRelationship inheritanceRelationship = new DMInheritanceRelationship();
            subClass.InheritanceSuperClassRelationship = inheritanceRelationship;
            subClass.InheritanceSuperClassRelationship.SuperClass = superClass;
            subClass.InheritanceSuperClassRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            subClass.InheritanceSuperClassRelationship.Save();
            subClass.Save();

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            IComboBox idComboBox = superClassControl.IDComboBox;
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(0, idComboBox.Items.Count);
            Assert.IsNotNull(superClass.ObjectIdentity);
            Assert.AreEqual(1, superClass.ObjectIdentity.Properties.Count);
            DMProperty identityProperty = superClass.ObjectIdentity.Properties[0];
            //---------------Execute Test ----------------------
            superClassControl.SubClass = subClass;
            //---------------Test Result -----------------------
            int subClassPropCount = subClass.Properties.Count;
            idComboBox = superClassControl.IDComboBox;
            Assert.AreEqual(subClassPropCount + 2, idComboBox.Items.Count,
                            "The Sub Classes Props plus the IDProp of the SuperClass plus blank");
            Assert.AreEqual("", idComboBox.Items[0].ToString());
            Assert.AreEqual(identityProperty.PropertyName, idComboBox.Items[1].ToString());
        }

        //This should be filled with the parent's ID prop + the child props.
        [Test]
        public void TestIDCombo_WhenHasSuperClass_HasIDProp_HasPropertiesOfSubClassWhereTypeIsSame()
        {
            //---------------Set up test pack-------------------
            DMClass superClass = TestUtilsShared.CreateSavedDMClassWithProps();
            TestUtilsShared.CreateSavedObjectIdentityInfo(superClass);
            DMClass subClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property = TestUtilsShared.CreateSavedDMProperty(subClass);
            TestUtilsDMProperty.SetDMPropIntType(property);
            DMInheritanceRelationship inheritanceRelationship = new DMInheritanceRelationship();
            subClass.InheritanceSuperClassRelationship = inheritanceRelationship;
            subClass.InheritanceSuperClassRelationship.SuperClass = superClass;
            subClass.InheritanceSuperClassRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            subClass.InheritanceSuperClassRelationship.Save();
            subClass.Save();

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            IComboBox idComboBox = superClassControl.IDComboBox;
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(0, idComboBox.Items.Count);
            Assert.IsNotNull(superClass.ObjectIdentity);
            Assert.AreEqual(1, superClass.ObjectIdentity.Properties.Count);
            DMProperty identityProperty = superClass.ObjectIdentity.Properties[0];
            //---------------Execute Test ----------------------
            superClassControl.SubClass = subClass;
            //---------------Test Result -----------------------
            int subClassPropCount = subClass.Properties.Count;
            idComboBox = superClassControl.IDComboBox;
            Assert.AreEqual(subClassPropCount + 2, idComboBox.Items.Count,
                            "The Sub Classes Props plus the IDProp of the SuperClass plus blank");
            Assert.AreEqual("", idComboBox.Items[0].ToString());
            Assert.AreEqual(identityProperty.PropertyName, idComboBox.Items[1].ToString());
        }

        [Test]
        public void Test_HasSuperClassCheckBox_WhenDMClassWithNoInheritanceRelationship_ShouldBeUnchecked()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();

            //---------------Assert Precondition----------------
            Assert.IsNull(bo.InheritanceSuperClassRelationship);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = bo;
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.HasSuperClassCheckBox.Checked);
            Assert.IsFalse(superClassControl.InheritanceRelationshipPanelInfo.Panel.Enabled);
        }

        [Test]
        public void Test_HasSuperClassCheckBox_WhenChangedToChecked_EnablesControls()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            superClassControl.SubClass = bo;
            //---------------Assert Precondition----------------
            Assert.IsFalse(superClassControl.HasSuperClassCheckBox.Checked);
            Assert.IsNull(bo.InheritanceSuperClassRelationship);
            Assert.IsFalse(superClassControl.InheritanceRelationshipPanelInfo.Panel.Enabled);
            Assert.IsNull(superClassControl.SubClass.InheritanceSuperClassRelationship);
            //---------------Execute Test ----------------------
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Test Result -----------------------
            Assert.IsTrue(superClassControl.HasSuperClassCheckBox.Checked);
            Assert.IsTrue(superClassControl.InheritanceRelationshipPanelInfo.Panel.Enabled);
            Assert.IsNotNull(superClassControl.SubClass.InheritanceSuperClassRelationship);
            Assert.AreSame(superClassControl.SubClass, superClassControl.SuperClassLookupComboBoxMapper.SubClass);
        }

        [Test]
        public void Test_HasSuperClassCheckBox_WhenChangedToChecked_ShouldCreateInheritanceRelationship()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            TestUtilsShared.CreateSavedDMClassWithProps();
            BusinessObjectCollection<DMClass> classesCol = BORegistry.DataAccessor.BusinessObjectLoader.GetBusinessObjectCollection<DMClass>("");
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            IComboBox superClassComboBox = superClassControl.SuperClassComboBox;
            ICheckBox hasSuperClassCheckBox = superClassControl.HasSuperClassCheckBox;
            DMClass subClass = TestUtilsShared.CreateUnsavedValidDMClass();
            superClassControl.SubClass = subClass;
            //--------------Assert Preconditions-----------------
            Assert.AreEqual(1, classesCol.Count);
            Assert.IsNull(superClassControl.InheritanceRelationship);
            Assert.IsFalse(hasSuperClassCheckBox.Checked);
            //---------------Execute Test ----------------------
            hasSuperClassCheckBox.Checked = true;
            //---------------Test Result -----------------------
            DMInheritanceRelationship inheritanceRelationship = superClassControl.InheritanceRelationship;
            Assert.IsNotNull(inheritanceRelationship);
            Assert.IsTrue(inheritanceRelationship.Status.IsNew);
            Assert.AreSame(subClass, inheritanceRelationship.SubClass);
            Assert.AreEqual(2, superClassComboBox.Items.Count, "The Class and a blank");
        }

        [Test]
        public void Test_HasSuperClassCheckBox_WhenChangedToChecked_AndNoSubClassSet_ShouldShowError()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            //---------------Assert Precondition----------------
            Assert.IsNull(superClassControl.SubClass);
            Assert.IsFalse(superClassControl.HasSuperClassCheckBox.Checked);
            //---------------Execute Test ----------------------
            try
            {
                superClassControl.HasSuperClassCheckBox.Checked = true;
                Assert.Fail("expected HabaneroArgumentException");
            }
                //---------------Test Result -----------------------
            catch (HabaneroArgumentException ex)
            {
                StringAssert.Contains("SubClass cannot be null", ex.Message);
                StringAssert.Contains("SubClass", ex.ParameterName);
            }
        }

        [Test]
        public void Test_HasSuperClassCheckBox_WhenChangedToUnchecked_ShouldDisablePanel()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            superClassControl.SubClass = bo;
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Assert Precondition----------------
            Assert.IsTrue(superClassControl.InheritanceRelationshipPanelInfo.Panel.Enabled);
            Assert.IsNotNull(bo.InheritanceSuperClassRelationship);
            //---------------Execute Test ----------------------
            superClassControl.HasSuperClassCheckBox.Checked = false;
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.InheritanceRelationshipPanelInfo.Panel.Enabled);
            Assert.IsFalse(superClassControl.HasSuperClassCheckBox.Checked);
            Assert.IsNull(bo.InheritanceSuperClassRelationship);
        }

        [Test]
        public void Test_HasSuperClassCheckBox_WhenChangedToUnchecked_ShouldDeleteTheInheritanceRelationship()
        {
            //---------------Set up test pack-------------------
            UITestUtils.SetupFixture(); // need to clear the lookup list, so reloading the classdefs
            DMClass dmClass2 = TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMClass();

            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();

            DMInheritanceRelationship superClassRelationship = new DMInheritanceRelationship();
            dmClass.InheritanceSuperClassRelationship = superClassRelationship;
            superClassRelationship.SuperClass = dmClass2;
            superClassRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            superClassRelationship.Save();
            dmClass.Save();

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory)
                                                      {SubClass = dmClass};
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.InheritanceSuperClassRelationship);
            Assert.AreSame(superClassRelationship, dmClass.InheritanceSuperClassRelationship);
            Assert.IsFalse(dmClass.InheritanceSuperClassRelationship.Status.IsDeleted);
            Assert.IsTrue(superClassControl.HasSuperClassCheckBox.Checked);
            //---------------Execute Test ----------------------
            superClassControl.HasSuperClassCheckBox.Checked = false;
            //---------------Test Result -----------------------
            Assert.IsFalse(superClassControl.HasSuperClassCheckBox.Checked);
            Assert.IsNull(dmClass.InheritanceSuperClassRelationship);
            Assert.IsTrue(superClassRelationship.Status.IsDeleted);
            Assert.IsTrue(superClassRelationship.Status.IsNew);
        }

        [Test]
        public void Test_HasSuperClassCheckBox_WhenChecked_ThenClickToAnotherClass_AndClickBackToOriginal_ShouldBeChecked()
        {
            //---------------Set up test pack-------------------
            DMClass originalClass = TestUtilsShared.CreateSavedDMClass();
            DMClass anotherClass = TestUtilsShared.CreateSavedDMClass();
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory)
                                                      {SubClass = originalClass};
            superClassControl.HasSuperClassCheckBox.Checked = true;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(superClassControl.InheritanceRelationship);
            Assert.IsNotNull(originalClass.InheritanceSuperClassRelationship);
            Assert.AreSame(superClassControl.InheritanceRelationship, originalClass.InheritanceSuperClassRelationship);
            //---------------Execute Test ----------------------
            superClassControl.SubClass = anotherClass;
            superClassControl.SubClass = originalClass;
            //---------------Test Result -----------------------
            Assert.IsNotNull(originalClass.InheritanceSuperClassRelationship);
            Assert.AreSame(originalClass, superClassControl.SubClass);
            Assert.AreSame(originalClass.InheritanceSuperClassRelationship, superClassControl.InheritanceRelationship);
            Assert.IsTrue(superClassControl.HasSuperClassCheckBox.Checked);
        }

        [Test]
        public void TestSelectingBlankSuperClassDoesNothing()
        {
            //---------------Set up test pack-------------------
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory);
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            superClassControl.SubClass = bo;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            superClassControl.SuperClassComboBox.SelectedIndex = -1;
            superClassControl.SuperClassComboBox.SelectedIndex = 0;
            //---------------Test Result -----------------------
        }

        [Test]
        public void Test_LoadIDCombo_WhenSuperClassDoesNotHaveObjectID_ShouldLoadSubClassesPropsOnly()
        {
            //---------------Set up test pack-------------------
            DMClass subClass = TestUtilsDMClass.CreateSavedDMClassWithNoOID(2);

            DMClass superClassWithID = TestUtilsDMClass.CreateSavedDMClassWithOID(0);

            DMClass superClassWithNoID = TestUtilsDMClass.CreateSavedDMClassWithNoOID(3);

            DMInheritanceRelationship inheritanceRelationship = TestUtilsShared.CreateUnsavedValidDMInheritanceRelationship();
            inheritanceRelationship.SubClass = subClass;
            inheritanceRelationship.SuperClass = superClassWithID;
            inheritanceRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory) {SubClass = subClass};
            IComboBox identityComboBox = superClassControl.IDComboBox;
            //---------------Assert Precondition----------------
            Assert.IsNull(superClassWithNoID.ObjectIdentity);
            Assert.AreEqual(3, superClassWithNoID.Properties.Count);
            Assert.AreEqual(2, subClass.Properties.Count);
            Assert.AreEqual(4, identityComboBox.Items.Count, "The two props, the superclass prop and the Blank");
            //---------------Execute Test ----------------------
            inheritanceRelationship.SuperClass = superClassWithNoID;
            //---------------Test Result -----------------------
            Assert.AreSame(subClass, inheritanceRelationship.SubClass);
            Assert.AreSame(superClassWithNoID, inheritanceRelationship.SuperClass);
            Assert.AreEqual(3, identityComboBox.Items.Count, "The two props and the Blank");
        }

        [Test]
        public void Test_LoadIDCombo_WhenSuperClassIsCompositePrimaryKey_ShouldRaiseErr()
        {
            //---------------Set up test pack-------------------
            DMClass subClass = TestUtilsDMClass.CreateSavedDMClassWithNoOID(2);

            DMClass superClassWithCompositeOID = TestUtilsDMClass.CreateSavedDMClassWithCompositeOID(2);

            DMInheritanceRelationship inheritanceRelationship = TestUtilsShared.CreateUnsavedValidDMInheritanceRelationship();
            inheritanceRelationship.SubClass = subClass;
            inheritanceRelationship.InheritanceType = ORMapping.ClassTableInheritance;

            new SuperClassControl(GlobalUIRegistry.ControlFactory){SubClass = subClass};
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, superClassWithCompositeOID.Properties.Count);
            Assert.AreEqual(2, superClassWithCompositeOID.ObjectIdentity.Properties.Count);
            //---------------Execute Test ----------------------
            try
            {
                inheritanceRelationship.SuperClass = superClassWithCompositeOID;
                Assert.Fail("Expected to throw an UserException");
            }
                //---------------Test Result -----------------------
            catch (UserException ex)
            {
                StringAssert.Contains(
                        "You cannot use a DMClass that has a Composite Primary Key in an Inheritance Relationship",
                        ex.Message);
            }
        }
        
        /*[Test]
        public void Test_LoadIDCombo_WhenSubClassIsCompositePrimaryKey_ShouldRaiseErr()
        {
            //---------------Set up test pack-------------------
            DMClass superClass = TestUtilsDMClass.CreateSavedDMClassWithNoOID(2);
            DMClass subClassWithCompositeOID = TestUtilsDMClass.CreateSavedDMClassWithCompositeOID(2);

            DMInheritanceRelationship inheritanceRelationship = TestUtilsShared.CreateUnsavedValidDMInheritanceRelationship();
            inheritanceRelationship.SuperClass = superClass;

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory) { SubClass = subClassWithCompositeOID };
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, subClassWithCompositeOID.Properties.Count);
            Assert.AreEqual(2, subClassWithCompositeOID.ObjectIdentity.Properties.Count);
            //---------------Execute Test ----------------------
            try
            {
                inheritanceRelationship.SubClass = subClassWithCompositeOID;
                Assert.Fail("Expected to throw an UserException");
            }
                //---------------Test Result -----------------------
            catch (UserException ex)
            {
                StringAssert.Contains(
                    "You cannot use a DMClass that has a Composite Primary Key in an Inheritance Relationship",
                    ex.Message);
            }
        }*/

        [Test]
        public void Test_LoadIDCombo_WhenSuperClassExistsWithSingleID_ShouldSetUpComboBoxWithAllProps()
        {
            //---------------Set up test pack-------------------
            DMClass subClass = TestUtilsDMClass.CreateSavedDMClassWithNoOID(2);

            DMClass superClass = TestUtilsDMClass.CreateSavedDMClassWithOID(1);

            DMInheritanceRelationship inheritanceRelationship = TestUtilsShared.CreateUnsavedValidDMInheritanceRelationship();
            inheritanceRelationship.SubClass = subClass;
            inheritanceRelationship.InheritanceType = ORMapping.ClassTableInheritance;

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory) { SubClass = subClass };
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, superClass.ObjectIdentity.Properties.Count);
            Assert.AreEqual(2, superClass.Properties.Count);
            Assert.AreEqual(2, subClass.Properties.Count);
            //---------------Execute Test ----------------------
            inheritanceRelationship.SuperClass = superClass;
            //---------------Test Result -----------------------
            Assert.AreSame(subClass, inheritanceRelationship.SubClass);
            Assert.AreSame(superClass, inheritanceRelationship.SuperClass);
            Assert.AreEqual(4, superClassControl.IDComboBox.Items.Count, "The two props, plus SuperClassID Prop and the Blank");
        }

        [Test]
        public void Test_LoadIDCombo_WhenSuperClassSetAndInheritanceTypeChangedToClassTable_ShouldSetUpComboBoxWithAllProps()
        {
            //---------------Set up test pack-------------------
            DMClass subClass = TestUtilsDMClass.CreateSavedDMClassWithNoOID(2);

            DMClass superClass = TestUtilsDMClass.CreateSavedDMClassWithOID(1);

            DMInheritanceRelationship inheritanceRelationship = TestUtilsShared.CreateUnsavedValidDMInheritanceRelationship();
            inheritanceRelationship.SubClass = subClass;
            inheritanceRelationship.InheritanceType = ORMapping.SingleTableInheritance;

            SuperClassControl superClassControl = new SuperClassControl(GlobalUIRegistry.ControlFactory) { SubClass = subClass };
            inheritanceRelationship.SuperClass = superClass;
            //---------------Assert Precondition----------------
            Assert.AreNotSame(ORMapping.ClassTableInheritance, superClassControl.InheritanceTypeComboBox.SelectedItem);
            Assert.AreSame(subClass, inheritanceRelationship.SubClass);
            Assert.AreSame(superClass, inheritanceRelationship.SuperClass);
            Assert.AreEqual(4, superClassControl.IDComboBox.Items.Count,
                            "The two props, plus SuperClassID Prop and the Blank");
            //---------------Execute Test ----------------------
            inheritanceRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            //---------------Test Result -----------------------
            Assert.AreSame(subClass, inheritanceRelationship.SubClass);
            Assert.AreSame(superClass, inheritanceRelationship.SuperClass);
            Assert.AreEqual(4, superClassControl.IDComboBox.Items.Count,
                            "The two props, plus SuperClassID Prop and the Blank");
        }

        [Test]
        [Ignore("This is for visual testing purposes")]
        public void Test_Visually()
        {
            //---------------Set up test pack-------------------

            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty();
            TestUtilsDMProperty.SetDMPropStringType(dmProperty);
            dmProperty.PropertyName = "MyDiscriminatorProp";
            dmProperty.Save();
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass superClass = (DMClass) dmProperty.Class;
            TestUtilsShared.CreateSavedDMProperty(superClass);
            TestUtilsShared.CreateSavedDMProperty(superClass);
            TestUtilsShared.CreateSavedDMProperty(superClass);
            TestUtilsShared.CreateSavedDMProperty(superClass);
            DMClass childClass = TestUtilsShared.CreateSavedDMClass(superClass.Assembly);
            dmInheritanceRelationship.SubClass = childClass;
            dmInheritanceRelationship.SuperClass = superClass;
            dmInheritanceRelationship.Discriminator = dmProperty.PropertyName;
            childClass.Save();
            dmInheritanceRelationship.Save();
            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            SuperClassControl superClassControl = new SuperClassControl(controlFactory)
            {
                SubClass = childClass
            };
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            TestUtilsUI.ShowInVisualTestingForm(controlFactory, superClassControl, Habanero.Faces.Base.DockStyle.Top);
            //---------------Test Result -----------------------
        }

        [Test]
        [Ignore("This is for visual testing purposes")]
        public void Test_Visually_WithImport()
        {
            //---------------Set up test pack-------------------
            const string xml = @"
                <classes>
                   <class name=""Address"" assembly=""ContactManager.BO"" displayName=""Address"" table=""Address"">
                      <property name=""AddressID"" type=""Guid"" assembly=""System"" 
                       displayName=""Address ID"" readWriteRule=""WriteNew"" databaseField=""AddressID""
                        description="""" 
                        compulsory=""true"" 
                      >
	                  </property>
                      <property name=""AddressType"" type=""String"" assembly=""System"" 
                       displayName=""Address Type"" readWriteRule=""ReadWrite"" databaseField=""AddressType""
                        description="""" 
                      >
	                  </property>
                      <property name=""AddressCategoryID"" type=""Guid"" assembly=""System"" 
                       displayName=""Address Category ID"" readWriteRule=""ReadWrite"" databaseField=""AddressCategoryID""
                        description="""" 
                      >
	                  </property>
                      <property name=""ContactID"" type=""Guid"" assembly=""System"" 
                       displayName=""Contact ID"" readWriteRule=""ReadWrite"" databaseField=""ContactID""
                        description="""" 
                      >
	                  </property>
                      <property name=""MailingListContactID"" type=""Guid"" assembly=""System"" 
                       displayName=""Mailing List Contact ID"" readWriteRule=""ReadWrite"" databaseField=""MailingListContactID""
                        description="""" 
                      >
	                  </property>
                    <primaryKey  >
                      <prop name=""AddressID"" />
                    </primaryKey>
                   </class>
                   <class name=""AddressDetails"" assembly=""ContactManager.BO"" displayName=""Address Details"" table=""AddressDetails"">
		                <superClass class=""Address"" assembly=""ContactManager.BO"" 
			                orMapping=""SingleTableInheritance"" discriminator=""AddressType""/>
                   </class>
                </classes>
                ";


            SolutionCreator solutionCreator = new SolutionCreator();
            DMSolution dmSolution = solutionCreator.CreateSolution();
            dmSolution.Assemblies.ForEach(assembly => assembly.MarkForDelete());
            XmlClassDefsLoader loader = new XmlClassDefsLoader("", new DtdLoader(), new FMDefClassFactory(dmSolution));
            loader.LoadClassDefs(xml);
            dmSolution.DefaultAssembly = dmSolution.Assemblies[0];
            DMSolutionPostLoadProcessor processor = new DMSolutionPostLoadProcessor(new AutoMockingUnityContainer());
            processor.ProcessSolution(dmSolution);
            dmSolution.Save();

            DMClass subClass = dmSolution.AllClasses.Find(dmClass => dmClass.ClassNameBO == "AddressDetails");

            IControlFactory controlFactory = GlobalUIRegistry.ControlFactory;
            SuperClassControl superClassControl = new SuperClassControl(controlFactory)
            {
                SubClass = subClass
            };
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            TestUtilsUI.ShowInVisualTestingForm(controlFactory, superClassControl, Habanero.Faces.Base.DockStyle.Top);
            //---------------Test Result -----------------------
        }


    }
}